package com.core.family.biz.yizhao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.common.domain.response.R;
import com.core.common.exception.BDException;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.family.admin.service.PlatFinanceStatementService;
import com.core.family.biz.yizhao.domain.dto.YzOrderDto;
import com.core.family.biz.yizhao.domain.po.YzOrder;
import com.core.family.biz.yizhao.domain.po.YzOrderPrice;
import com.core.family.biz.yizhao.mapper.YzOrderMapper;
import com.core.family.biz.yizhao.service.YzOrderPriceService;
import com.core.family.biz.yizhao.service.YzOrderService;
import com.core.family.biz.yizhao.service.YzOrderTakeService;
import com.core.family.biz.yizhao.service.YzWalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.core.common.gen.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-7-1 8:26:43
 * @Description 服务实现层 一招订单表
 */
@Slf4j
@Service
public class YzOrderServiceImpl extends BaseServiceImpl<YzOrderMapper, YzOrder> implements YzOrderService {
    @Autowired
    YzOrderPriceService priceService;

    @Autowired
    YzWalletService walletService;

    @Autowired
    PlatFinanceStatementService statementService;

    @Autowired
    YzOrderTakeService takeService;


    @Override
    public IPage getDetailPage(PageQuery<BaseQuery> baseQuery) {
        IPage<YzOrder> page=selectPage(baseQuery);

        return null;
    }

    @Override
    public YzOrderDto getDetail(Integer id) {
        YzOrder order=this.baseMapper.selectById(id);
        YzOrderDto dto=new YzOrderDto();
        BeanUtils.copyProperties(order,dto);
        return dto;
    }

    @Transactional()
    @Override
    public boolean selectPrice(YzOrder bean) {
        YzOrderPrice price = priceService.getWithDetail(bean.getOrderPriceId());

        /*补全接单人信息*/
        bean.setOrderPriceId(price.getId());
        bean.setOrderTakeName(price.getMasterName());
        bean.setOrderTakePhone(price.getMasterHone());
        bean.setOrderTakeRolename(price.getMasterRolename());
        bean.setOrderTakeTime(new Date());
        bean.setPrice(price.getPriceTotal());
        /* 修改状态*/
        bean.setStatus("服务中");



        statementService.abmt(bean.getOrderUserId(),price.getUserId(),bean.getPrice(),bean.getProjectName(),bean.getId(),true);


        takeService.updateByUserIdAndOrderId(price.getUserId(),bean.getId(),"服务中");

        return this.baseMapper.updateById(bean) > 0;
    }

    @Transactional()
    @Override
    public boolean done(Integer id) {
        YzOrder dbOrder = getById(id);
        if (null == dbOrder) {
           return false;
        }
        YzOrder bean = new YzOrder();
        bean.setId(id);
        bean.setStatus("已完成");

        walletService.unFrozenAmount(bean.getOrderTakeUserId(),bean.getPrice());
        return  updateById(bean);
    }

    private List<String> columnList=new ArrayList<>();
    @Override
    protected List<String> setFsc() {
        columnList.add("kindName");
        columnList.add("orderTakeName");
        columnList.add("owerName");
        columnList.add("productName");
        columnList.add("projectAddress");
        columnList.add("projectName");
        return columnList;
        
    }
}

